#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from Public.BasePage import BasePage
# from Public.maxim_monkey import Maxim
from Public.Decorator import *
from random import randint
from uiautomator2 import UiObjectNotFoundError


class WxOAListPage(BasePage):
    @teststep
    def wait_page(self):
        try:
            if self.d(resourceId="android:id/text1").wait(timeout=15):
                if self.d(resourceId="android:id/text1").get_text() == '公众号':
                    log.i('成功进入微信公众号列表页')
                    pass
            else:
                raise Exception('Not in HonePage')
        except Exception:
            raise Exception('Not in HonePage')

    @teststep
    def click_add_icon(self):
        '''
        点击顶部右上角的 +
        :param inst: tab的位数
        :return:
        '''
        ele = self.d.xpath('//android.support.v7.widget.LinearLayoutCompat/android.widget.LinearLayout[2]')
        ele.click()
        time.sleep(randint(5, 20))

    @teststep
    def search_OA(self, text):
        '''
        搜索公众号
        :param inst: tab的位数
        :return:
        '''
        log.i('输入公众号ID:%s' % text)
        self.d(resourceId="com.tencent.mm:id/m7").click()
        self.d(resourceId="com.tencent.mm:id/m7").clear_text()
        self.d(resourceId="com.tencent.mm:id/m7").send_keys(text)
        self.d.send_action("search")
        time.sleep(randint(5, 20))
        log.i('点击进入搜索的公众号')
        self.d.xpath('//*[@resource-id="search_result"]/android.view.View[2]').click()
        time.sleep(randint(5, 20))

    @teststep
    def follow_OA(self):
        '''
        关注公众号
        :param inst: tab的位数
        :return:
        '''
        log.i('关注公众号')
        if self.d(text="进入公众号").wait(timeout=15):
            log.i('已关注公众号，直接进入公众号')
            self.d(text="进入公众号").click()
        else:
            log.i('没关注公众号，先关注公众号')
            self.d(resourceId="com.tencent.mm:id/b93").click()
        time.sleep(10)

    @teststep
    def publish_OA(self, text):
        '''
        关注公众号
        :param inst: tab的位数
        :return:
        '''
        log.i('进入公众号后台')
        if self.d(resourceId="com.tencent.mm:id/aqd").wait(timeout=10):
            log.i("直接发送文案")
            # self.d(resourceId="com.tencent.mm:id/aqd").send_keys(text)
            self.d(resourceId="com.tencent.mm:id/aqd").click()
            self.d.send_keys(text, clear=True)
            time.sleep(randint(5, 20))
            self.d(resourceId="com.tencent.mm:id/aql").click()
        else:
            log.i("切换输入框，然后发送输入文案")
            self.d(resourceId="com.tencent.mm:id/aqz").click()
            self.d(resourceId="com.tencent.mm:id/aqd").click()
            # self.d(resourceId="com.tencent.mm:id/aqd").send_keys(text)
            self.d.send_keys(text, clear=True)
            time.sleep(randint(5, 20))
            self.d(resourceId="com.tencent.mm:id/aql").click()

    @teststep
    def comment_OA(self, text):
        '''
        评论首篇公众号
        :param inst: tab的位数
        :return:
        '''
        # 点击右上角的 "..." , 进入公众号主页
        self.d.xpath('//android.support.v7.widget.LinearLayoutCompat').click()
        time.sleep(randint(5, 15))
        # 点击首文
        self.d(resourceId="com.tencent.mm:id/b6m").click()
        time.sleep(randint(5, 20))
        # 点击文章右上角的 "..."
        self.d.xpath('//android.support.v7.widget.LinearLayoutCompat').click()
        # 收藏文章
        self.d(resourceId="com.tencent.mm:id/dd", text="收藏").click()
        time.sleep(randint(5, 20))
        # 寻找写留言的入口
        for i in range(1, 50):
            if self.d(resourceId="js_cmt_write2").exists or self.d(resourceId="js_cmt_write1").exists:
                break
            self.swipe_up()
            time.sleep(2)
        if self.d(resourceId="js_cmt_write2").exists:
            log.i("已经找到写留言的入口(没有任何留言的情况)")
            self.d(resourceId="js_cmt_write2").click()
            # 写评论
            self.d.send_keys(text, clear=True)
            time.sleep(randint(10, 30))
            # 提交
            self.d(resourceId="com.tencent.mm:id/dsf").click()
            time.sleep(randint(2, 7))
            self.d(resourceId="com.tencent.mm:id/dsd").click()
        elif self.d(resourceId="js_cmt_write1").exists:
            log.i("已经找到写留言的入口（有部分留言的情况）")
            self.d(resourceId="js_cmt_write1").click()
            # 写评论
            self.d.send_keys(text, clear=True)
            time.sleep(randint(10, 30))
            # 提交
            self.d(resourceId="com.tencent.mm:id/dsf").click()
            time.sleep(randint(2, 7))
            self.d(resourceId="com.tencent.mm:id/dsd").click()
        else:
            log.i("没有找到写留言的入口")

    @teststep
    def back_search_page(self):
        '''
        从公众号退至搜索界面
        :param inst: tab的位数
        :return:
        '''
        for i in range(1, 4):
            if self.d(resourceId="com.tencent.mm:id/m7").exists:
                break
            self.d.press('back')
            time.sleep(randint(5, 10))
        time.sleep(randint(60, 120))

    @teststep
    def back_search_page_fr_article(self):
        '''
        从公众号文章退至搜索界面
        :param inst: tab的位数
        :return:
        '''
        for i in range(1, 6):
            if self.d(resourceId="com.tencent.mm:id/m7").exists:
                break
            self.d.press('back')
            time.sleep(randint(5, 10))
        time.sleep(randint(60, 300))


if __name__ == '__main__':
    from Public.Log import Log

    WxOAListPage().set_driver(None)
    WxOAListPage().click_add_icon()
